﻿<script src="https://cdn.bootcss.com/moment.js/2.24.0/moment.js"></script>
<script>
var ProjectList=[];
var TeamList=[];

var requrl = "/pwccnhk/gtsventure/customapi/RetrieveQueryDocs";
function GetProjectList(){
    
    var Jsondata = {};
    Jsondata.SearchItem = [];
    Jsondata.EntityName = "7m2ncu0h5i";
    Jsondata.index = 0;
    Jsondata.size = 100;
    Jsondata.Columns =["_id","ProjectNameNew"];
    Jsondata.SearchItem.push({ "Name": "IsDeleted", "Value": 0, "Operator": 'Equal', 'Method': 'AND' });    
    $.ajax({
        url: requrl,
        type: 'POST',
        data: Jsondata,
        async: true,
        success: function (response) {
            if (response.StatusCode === 200) {
                if (response.Data.total > 0) { 
                    $.each(response.Data.rows,function(index,item){
                        var obj={
                            projectCode:item._id
                            ,ProjectName:item.ProjectNameNew
                        };
                        ProjectList.push(obj);
                       
                    });
                    console.log("GetProjectList",ProjectList);
                }
            }
            return [];
        }
    });
    return ProjectList;
};



function GetTeams(){   
    var Jsondata = {};
    Jsondata.SearchItem = [];
    Jsondata.EntityName = "8gtrb625m0h";
    Jsondata.index = 0;
    Jsondata.size = 100;
    Jsondata.Columns =["_id","TeamName","Metadata-TeamMembers_relationsvalue","TeamMembers"];
    Jsondata.SearchItem.push({ "Name": "IsDeleted", "Value": 0, "Operator": 'Equal', 'Method': 'AND' }); 
    $.ajax({
        url: requrl,
        type: 'POST',
        data: Jsondata,
        async: true,
        success: function (response) {
            if (response.StatusCode === 200) {
                if (response.Data.total > 0) { 
                    $.each(response.Data.rows,function(index,item){
                        var obj={
                            TeamCode:item._id
                            ,TeamName:item.TeamName
                            ,TeamMenberOjbects:item["Metadata-TeamMembers_relationsvalue"]
                            ,TeamMembers:item.TeamMembers
                        };
                        TeamList.push(obj);
                       
                    });
                    console.log("GetTeams",TeamList);
                    GetResources("9dc0e399f5bf4615bff403fef0b9802d");
                    GetEvents("", "", "08/26/2019", "11/30/2019");
                }
            }
            return [];
        }
    });
    return TeamList;
}
  
 
function GetResources(teamcode){
    var ResourceList=[];
    $.each(TeamList,function(i,o){
        if(teamcode==""||o.TeamCode==teamcode){
            $.each(o.TeamMenberOjbects,function(j,k){
                ResourceList.push({
                    "id":k.StaffId,
                    "title":k.StaffName,
                    "eventColor":"orange"
                });
            });
        };
    });
    console.log("GetResources",ResourceList);
    return ResourceList;
}



function GetEvents(projectCode, teamCode, startDate, endDate) {  
    var Jsondata = {};
    Jsondata.SearchItem = [];
    Jsondata.EntityName = "3l6xvhhvcxp";
    Jsondata.index = 0;
    Jsondata.size = 100;
    Jsondata.SearchItem.push({ "Name": "IsDeleted", "Value": 0, "Operator": 'Equal', 'Method': 'AND' });

    if (IsNotNull(projectCode)) {
        Jsondata.SearchItem.push({ "Name": "ProjectCode", "Value": projectCode, "Operator": 'Equal', 'Method': 'AND' });
    }

    if (IsNotNull(teamCode)) {
        var tm = GetResources(teamCode);
        var staffList=[];
        $.each(tm,function(i,o){
            staffList.push(o.id);
        });
        if (staffList.length > 0) {
            Jsondata.SearchItem.push({ "Name": "Person", "Value": staffList.join("|"), "Operator": 'Contains', 'Method': 'AND' });
        }
    }

    if (IsNotNull(startDate) && IsNotNull(endDate)) {
        Jsondata.SearchItem.push({
            "Method": "AND",
            "Name": "",
            "Operator": "",
            "SubSearchItems": [{
                "Method": "OR",
                "Name": "StartDate",
                "Operator": "Range",
                "SubSearchItems": null,
                "Value": new Date(new Date(startDate).toISOString()).getTime() + "|" + new Date(new Date(endDate).toISOString()).getTime()
            },
            {
                "Method": "OR",
                "Name": "EndDate",
                "Operator": "Range",
                "SubSearchItems": null,
                "Value": new Date(new Date(startDate).toISOString()).getTime() + "|" + new Date(new Date(endDate).toISOString()).getTime()
            }]
        });
    }

    Jsondata.Columns = ["_id", "Project","ProjectCode","Metadata-Person_relationsvalue","Person", "Task", "StartDate", "EndDate"];
    console.log("GetEvents-parameters",Jsondata);
    var result = [];
    $.ajax({
        url: requrl,
        type: 'post',
        data: Jsondata,
        success: function (response) {
            if (response.StatusCode === 200) {               
                if (response.Data.total > 0) {
                    $.each(response.Data.rows,function(i,o){
                        result.push({
                            resourceId:o.Person[0],
                            title:IsNotNull(projectCode)?o.Task:o.Project,
                            start:moment(o.StartDate).format("YYYY-MM-DD"),
                            end:moment(o.EndDate).format("YYYY-MM-DD")
                            });
                    });
                    console.log("GetEvents-results",result);
                }
            }
        }
    });
    return result;
}

function IsNotNull(value) {
    return value !== "" && value !== null && value !== undefined ? true : false;
}

$(function(){
    GetTeams();
    GetProjectList();
});
</script>